package pers.xhh.frontapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.xhh.frontapi.annotation.UserLoginToken;
import pers.xhh.frontapi.common.Constant;
import pers.xhh.frontapi.common.Mi;
import pers.xhh.frontapi.common.ResultCode;
import pers.xhh.frontapi.entity.to.user.*;
import pers.xhh.frontapi.service.MiUserService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author wei-xhh
 * @date 2020-11-30
 */
@CrossOrigin
@RestController
@RequestMapping("/mi/api")
public class MiUserController {

    @Autowired
    MiUserService miUserService;

    @PostMapping("/login")
    public Mi login(@Valid @RequestBody MiUserLogin miUserTo) {
        return miUserService.login(miUserTo);
    }

    @PostMapping("/register")
    public Mi register(@Valid @RequestBody MiUserTo miUserTo) {
        return miUserService.register(miUserTo);
    }

    @GetMapping("/oauth2.0/weibo")
    public void weibo(@RequestParam("code") String code, HttpServletResponse response) throws Exception {
        response.sendRedirect("http://42.194.142.165:8080/#/?code=" + code);
    }

    @GetMapping("/oauth2.0/weibo/success")
    public Mi weiboSuccess(@RequestParam("code") String code) throws Exception {
        return miUserService.weibo(code);
    }

    @GetMapping("/weibo/login")
    public Mi weiboLogin(HttpServletResponse response){
        String url = "https://api.weibo.com/oauth2/authorize?client_id=1799353902&response_type=code&redirect_uri="
                + Constant.REDIRECT_URI;
        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Mi.error(ResultCode.ERROR900.getMessage(), ResultCode.ERROR900.getStatus());
    }

    @GetMapping("/oauth2.0/fail")
    public Mi fail(){
        return Mi.error(ResultCode.ERROR900.getMessage(), ResultCode.ERROR900.getStatus());
    }

    @UserLoginToken
    @GetMapping("/users/info")
    public Mi getUser() {
        return miUserService.getUser();
    }

    @UserLoginToken
    @PutMapping("/users/put")
    public Mi putUser(@RequestBody(required = false) MiUserPutTo miUserPutTo) {
        return miUserService.putUser(miUserPutTo);
    }

    @UserLoginToken
    @PostMapping("/users/put/pwd")
    public Mi updatePassword(@Valid @RequestBody UserPwd userPwd) {
        return miUserService.updatePassword(userPwd);
    }

    @UserLoginToken
    @PostMapping("/users/upload/avatar")
    public Mi uploadAvatar(@RequestParam("file") MultipartFile file) throws IOException {
        return miUserService.uploadAvatar(file);
    }

    @PostMapping("/users/auth")
    public Mi auth(@Valid @RequestBody AuthTo authTo) throws UnsupportedEncodingException, JsonProcessingException {
        return miUserService.auth(authTo);
    }

    @UserLoginToken
    @PostMapping("/users/find/pwd")
    public Mi findPwd(@Valid @RequestBody UserFindPwd userFindPwd){
        return miUserService.findPwd(userFindPwd);
    }

    @UserLoginToken
    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }
}
